<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" session="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
    <meta name="renderer" content="webkit">
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <title>智能锁管理</title>

    <link href="${pageContext.request.contextPath}/static/css/common.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/css/bootstrap.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/font-awesome-4.3.0/css/font-awesome.min.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/css/select2.min.css"
          rel="stylesheet">
    <!-- jqgrid-->
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/custom.ui.jqgrid.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/layer/skin/layer.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/plugins/jqGrid/css/style.css" rel="stylesheet">
	 <link href="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/css/select2.min.css"
          rel="stylesheet">
    <script src="${pageContext.request.contextPath}/static/js/jquery-1.11.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery.form.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/common.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/bootstrap-3.3.5/js/bootstrap.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/i18n/grid.locale-cn.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/jqGrid/js/minified/jquery.jqGrid.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/plugins/layer/layer.js" type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/static/js/global.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugins/select2-4.0.0/dist/js/select2.min.js"></script>
	<script type="text/javascript"
            src="${pageContext.request.contextPath}/static/plugins/My97DatePicker/WdatePicker.js"></script>
    <style type="text/css">
        .main h5 a {
            position: absolute;
            right: 20px;
            font-size: 14px;
            bottom: 0px;
            display: inline-block;
            padding: 5px;
        }

        .table_list tbody tr {
            height: 35px;
            text-align: center;
        }
        .left ul li{

			*line-height:40px;

			*padding-left:20px;
		}

		.left ul li a, .left ul li a:hover{
			*color:#fff;
		}
		.panel{
			*+margin-left:270px;
		}
		.lt{
			float:left;
		}
		.form-group{
			*+overflow:hidden;
		}
		.input-group .form-control{
			width:78px;
			width:100px\9;
			*+width:100px;
		}
		.form-control{
			*+padding:0;

		}
		.btn{
			*+width:60px;
		}
		.jqGrid_wrapper{
			*+margin-left:270px;
		}
		.input-group-addon{
			width:40px;
			width:40px\9;
			*+width:30px;
			height:34px;
			height:34px\9;
			line-height:20px\9;
			*+height:22px;
		}
		.selectlabe1,.selectlabe2{*width:60px !important}
    </style>
</head>
<body view="menu-item-4142">
<jsp:include page="../../top.jsp"></jsp:include>
<div class="right">
 	<div class="wrapper">
        <div class="panel panel-default">
            <div class="panel-heading">
                <span><strong>查询条件</strong></span>

                <div class="ibox-tools">
                    <a class="collapse-link"> <i class="fa fa-chevron-down"></i></a>
                    <a class="collapse-link  d-n"> <i class="fa fa-chevron-up"></i></a>
                </div>
            </div>
            <div class="panel-body">
                <form class="form-horizontal">
         			<c:if test="${rLevel == 4}">
         				<div class="form-group">
		                    <label class="selectlabe1 col-md-1 form-lable lt">锁PID：</label>
                        	<div class="selectdivs col-md-3 lt" >
                            	<input id="pid" name="pid" type="text" class="form-control"/>
                        	</div>
	                        <div class="col-md-1 form-action lt">
	                            <button class="btn btn-success form-control lt" id="query">查询</button>
	                        </div>
	                        <div class="col-md-1 form-action lt">
	                            <button class="btn btn-success form-control" id="resetForm">清空</button>
	                        </div>
                    	</div>
         			</c:if>

         			<c:if test="${rLevel == 3}">
         				<div class="form-group">
         					<label class="selectlabe1 col-md-1 form-lable lt" style="width: 90px;">主用户：</label>
		                    <div class="selectdivs col-md-3 lt" >
		                        <select name="communityId" id="communityId" class="form-control col-md-1" style="width: 150px;"></select>
		                    </div>
		                    <label class="selectlabe2 col-md-1 form-lable lt" >所属区域：</label>
				            <div id="region" class="col-md-3 lt" style="width: 130px;padding-left: 0px;">
				                <script type="text/javascript">
				                    loadCity();
				                </script>
				            </div>
				              <label class="selectlabe1 col-md-1 form-lable lt">锁PID：</label>
	                        <div class="col-sm-3 lt">
	                            <div class="input-group">
	                                <input class="form-control col-md-1 lt" name="pid1" id='pid1'/>

	                                <div class="input-group-addon lt">to</div>
	                                <input class="form-control col-md-1 lt" name='pid2' id='pid2'/>
	                            </div>
	                        </div>
                    	</div>

                    	<div class="form-group">
	                        <label class="selectlabe1 col-md-1 form-lable lt">安装状态：</label>
	                        <div class="selectdivs col-sm-3 lt">
	                            <div class="div2 input-group lt">
	                                <select name="status" id="status" class="form-control col-md-1" style="width: 150px;">
		                                <option value="">请选择</option>
		                                <option value="0">未安装</option>
		                                <option value="1">已安装</option>
		                                <option value="2">已测试安装</option>
	                            	</select>
	                            </div>
	                        </div>
	                      	<label class="selectlabe2 col-md-1 form-lable lt">检测状态：</label>
	                        <div class="selectdivs col-md-3 lt" >
	                            <div class="div2 input-group">
	                                <select name="checkstatus" id="checkstatus" class="form-control col-md-1" style="width: 130px;">
		                                <option value="">请选择</option>
		                                <option value="0">未检测</option>
		                                <option value="1">合格</option>
		                                <option value="2">不合格</option>
	                            	</select>
	                            </div>
	                        </div>

		                       <div class="col-md-1 form-action lt">
		                            <button class="btn btn-success form-control" id="query">查询</button>
		                        </div>
		                        <div class="col-md-1 form-action lt">
		                            <button class="btn btn-success form-control" id="resetForm">清空</button>
		                        </div>
	                      </div>
         			</c:if>
 					<c:if test="${userRole == 1 || userRole == 2 || userRole == 21}">
		                <div class="form-group">
		               		<label class="selectlabe1 col-md-1 form-lable lt">公司名称：</label>
		                    <div class="selectdivs col-md-3 lt">
		                        <select name="agtCode" id="agtCode" class="form-control col-md-1"></select>
		                    </div>

		                    <label class="selectlabe2 col-md-1 form-lable lt" style="width: 90px;">小区名称：</label>
		                    <div class="selectdivs col-md-3 lt" >
		                        <select name="communityId" id="communityId" class="form-control col-md-1"></select>
		                    </div>
		                    <label class="selectlabe2 col-md-1 form-lable lt" >所属区域：</label>
				            <div id="region" class="col-md-3 lt" style="width: 16%;padding-left: 0px;">
				                <script type="text/javascript">
				                    loadCity();
				                </script>
				            </div>
		         		</div>
		         		<div class="form-group">
				                      <label class="selectlabe1 col-md-1 form-lable lt">锁PID：</label>
                        	<div class="selectdivs col-md-3 lt" >
                            	<input id="pid" name="pid" type="text" class="form-control"/>
                        	</div>
	                        <div class="col-md-1 form-action lt">
	                            <button class="btn btn-success form-control" id="query">查询</button>
	                        </div>
	                        <div class="col-md-1 form-action lt">
	                            <button class="btn btn-success form-control" id="resetForm">清空</button>
	                        </div>
	                      </div>
         			</c:if>
                </form>
            </div>
        </div>
	    <div class="jqGrid_wrapper">
	        <div id="jqGrid_operation" class="jg-grid">
	            <div class="jqGrid_operation" id="action"></div>
	        </div>
	        <table id="jqGrid"></table>
	        <div id="jqGrid_paper"></div>
	    </div>
	</div>
</div>
<script>
    $(document).ready(
            function () {
                var ifUp = false;
                var ifDown = false;
                var ifTestKey = false;
            	$.ajax({
                    type: "POST",
                    async: false,
                    url: '/admin/auth_menusAll.action',
                    data: "url=" + window.location.pathname,
                    success: function (data) {
                        data = eval("(" + data + ")");
                        for (var i in data) {
                            var action = data[i].mName;
                            switch (action) {
                                case "品质回收管理":
                                    $("#jqGrid_operation").append("<a style=\"border-bottom:1px solid #337ab7;margin-left: 14px;font-size: 16px;\" href=\"${pageContext.request.contextPath}/admin/tlocks_recoveryListView2.action\">品质回收管理</a>");
                                    break;
                            }
                        }
                    }
                });
            	  $.ajax({
                      type: "POST",
                      async: false,
                      url: '/admin/auth_buttons.action',
                      data: "url=" + window.location.pathname,
                      success: function (data) {
                          data = eval("(" + data + ")");
                          for (var i in data) {
                              var action = data[i].mName;
                              switch (action) {
	                              case "智能锁新增":
	                                  $("#action").append("<a id=\"add\"><i class=\"glyphicon glyphicon-plus\"></i>添加</a>");
	                                  $("#add").click(function () {
	                                      layer.open({
	                                          type: 2,
	                                          title: ['智能锁新增', 'font-weight:bold'],
	                                          shadeClose: true, //点击遮罩关闭层
	                                          area: ['800px', '430px'],
	                                          scrollbar: false,
	                                          content: '${pageContext.request.contextPath}/admin/tlocks_addView.action',
	                                          btn: ['提交', '取消'],
	                                          yes: function (index, layero) {
	                                              var options = {
	                                                  success: function (data) {
	                                                      data = JSON.parse(data);
	                                                      if (data.code == 1) {
	                                                          layer.alert(data.msg, function () {
	                                                              layer.closeAll();
	                                                              refreshGrid();
	                                                          });
	                                                      } else {
	                                                          layer.alert(data.msg);
	                                                      }
	                                                  }
	                                              };
	                                             	$("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
	                                            	$("#layui-layer-iframe" + index).contents().find("#form").submit();
	                                          },
	                                          cancel: function (index, layero) {
	                                              layer.close(index);
	                                          }
	                                      });
	                                  });
	                              break;
                              	  case "智能锁检验合格":
                                      $("#action").append("<a id=\"up\"><i class=\"glyphicon glyphicon-thumbs-up\"></i>合格</a>");
                                      ifUp = true;
                                      $("#up").click(function () {
                                          var sels = jQuery("#jqGrid").jqGrid('getGridParam', 'selarrrow');
                                          var rowData = $("#jqGrid").jqGrid("getRowData");
                                          if (sels == "") {
                                              layer.alert("请选择要检验为合格的记录", {
                                                  icon: 6,
                                                  closeBtn: false
                                              });
                                          } else {
                                        	  var ifChange= true;
                                              for (var i = 0; i < rowData.length; i++) {
                                            	  if(rowData[i].STATUS == '已安装'){
                                            		  ifChange = false
                                       	    		  break;
                                         	    	}
                                       	    	}
                                     	    	if(ifChange){
                                        	  		check(sels.toString(),1);
                                         	    }else{
                                         	    	layer.alert("包含已安装智能锁，无法批量修改状态", {
                                                        icon: 6,
                                                        closeBtn: false
                                                    });
                                             	}
                                          }
                                      });
                                      break;
                                  case "智能锁检验不合格":
                                      $("#action").append("<a id=\"down\"><i class=\"glyphicon glyphicon-thumbs-down\"></i>不合格</a>");
                                      ifDown = true;
                                       $("#down").click(function () {
                                          var sels = jQuery("#jqGrid").jqGrid('getGridParam', 'selarrrow');
                                          var rowData = $("#jqGrid").jqGrid("getRowData");
                                          if (sels == "") {
                                              layer.alert("请选择要检验为不合格的记录", {
                                                  icon: 6,
                                                  closeBtn: false
                                              });
                                          } else {
                                        	  var ifChange= true;
                                              for (var i = 0; i < rowData.length; i++) {
                                            	  if(rowData[i].STATUS == '已安装'){
                                            		  ifChange = false
                                       	    		  break;
                                         	    	}
                                       	    	}
                                     	    	if(ifChange){
                                     	    		check(sels.toString(),2);
                                         	    }else{
                                         	    	layer.alert("包含已安装智能锁，无法批量修改状态", {
                                                        icon: 6,
                                                        closeBtn: false
                                                    });
                                             	}
                                          }
                                      });
                                      break;
                                  case "导出激活码":
                                      $("#action").append("<a id=\"export\"><i class=\"glyphicon glyphicon-edit\"></i>导出激活码</a>");
                                       $("#export").click(function () {
                                    	   layer.open({
                                               type: 2,
                                               title: ['导出激活码', 'font-weight:bold'],
                                               shadeClose: true, //点击遮罩关闭层
                                               area: ['800px', '300px'],
                                               scrollbar: false,
                                               content: '${pageContext.request.contextPath}/admin/tlocks_exportIndex.action',
                                               btn: ['提交', '取消'],
                                               yes: function (index, layero) {
                                  	            	var options = {
                                  	                    success: function (data) {
//                                  	                        data = JSON.stringify(data);
                                                            console.log(data)
                                  	                        if (data.code == 1) {
                                  	                            layer.alert(data.msg, function (index) {
                                                                    console.log(data.url)
                                  	                            	window.open(data.url);//打开链接
                                                                    layer.close(index);
                                  	                            });
                                  	                        } else {
                                  	                            layer.alert(data.msg);
                                  	                        }
                                  	                    }
                                  	                };
                                  	            	$("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
                                                    $("#layui-layer-iframe" + index).contents().find("#form").submit();

                                               },
                                               cancel: function (index, layero) {
                                                   layer.close(index);
                                               }
                                           });
                                      });
                                      break;
                                  case "发放测试钥匙":
                                      ifTestKey = true;
                                      break;
                              }
                          }
                      }
                  });
                $.jgrid.defaults.styleUI = "Bootstrap";

                if('${rLevel}'==4){
                	$("#jqGrid").jqGrid(
                            {
                                url: "${pageContext.request.contextPath}/admin/tlocks_page.action?type=1",
                                mtype: 'POST',
                                datatype: "json",
                                height: 350,
                                autowidth: true,
                                shrinkToFit: true,
                                rowNum: 10,
                                rowList: [10, 20, 30],
                                colNames: ['序号', '所属区域', '公司', '主用户',
                                    '最近一次开门','PID ','安装时间','操作'],
                                colModel: [{
                                    name: "SID",
                                    index: "SID",
                                    key: true,
                                    hidden: true
                                }, {
                                    name: 'CITYCODE',
                                    index: 'CITYCODE',
                                    width: 90
                                }, {
                                    name: 'AGTNAME',
                                    index: 'AGTNAME',
                                    width: 80
                                },{
                                    name: 'COMMUNITYNAME',
                                    index: 'COMMUNITYNAME',
                                    width: 80
                                }, {
                                    name: "OTIME",
                                    index: "OTIME",
                                    width: 80,
                                    formatter: function (cellvalue, options, rawObject) {
                                    	if(cellvalue=='no' || !cellvalue){
                                        	return '';
                                        }else if(rawObject.STATUS!='1'){
                                    		return '';
                                    	}else{
                                        	return cellvalue;
                                       	}
                                    }
                                },{
                                    name: 'PID',
                                    index: 'PID',
                                    width: 80
                                },{
                                    name: "INSTALLTIME",
                                    index: "INSTALLTIME",
                                    width: 80
                                },{
                                    name: 'CHECKSTATUS',
                                    index: 'CHECKSTATUS',
                                    width: 100,
                                    formatter: function (cellvalue, options, rawObject) {
                                    	if(cellvalue=='no'){
                                        	return '';
                                        }else if('${userRole}'==1 || '${userRole}'== 2){
                                        	if(cellvalue=='0' && rawObject.STATUS=='2'){
                                        		return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                       + rawObject.SID + ',1)"/><input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                       + rawObject.SID + ',2)"/>';
                                            }else if(cellvalue=='1' && rawObject.STATUS=='2'){
                                                return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                            }else if(cellvalue=='2' && rawObject.STATUS=='2'){
                                                return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                        + rawObject.SID + ',1)"/>';
                                            }else{
                                            	return '';
                                            }
                                        }else if('${userRole}'==3 || '${userRole}'== 4){
                                        	if(cellvalue=='0' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifUp){
                                                   value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                       + rawObject.SID + ',1)"/>';
                                                }
                                                if(ifDown){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                                }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else if(cellvalue=='1' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifDown){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                                }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else if(cellvalue=='2' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifUp){
                                                    value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                        + rawObject.SID + ',1)"/>';
                                                 }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else{
                                            	return '';
                                            }
                                        }else if(('${userRole}'==5 || '${userRole}'== 6) && ifTestKey){
                                        	return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="发放钥匙" onclick="testKey2('
                                                    + rawObject.SID + ')"/>';
                                        }else{
                                        	return '';
                                        }
                                 	}
                                }],
                                sortname: 'status desc,sid',
                                sortorder:'desc',
                                rownumbers: true,
                                jsonReader: {
                                    root: 'rows',
                                    page: "page",
                                    total: 'total',
                                    records: "records",
                                    repeatitems: false
                                },
                                pager: "#jqGrid_paper",
                                viewrecords: true,
                                multiselect: true,
                                hidegrid: false,
                                loadComplete: function(){
                            		var re_records = Number($("#jqGrid").getGridParam('records'));
                            		var page = Number($("#jqGrid").getGridParam('page'));
                            		var rows=Number($("#jqGrid").getGridParam('rowNum'));
                            		var num = re_records-(page-1)*rows;
                            		if(num < rows)
                            		{
                            			for(var i=num;i<rows;i++){
                            				$("#jqGrid").jqGrid("addRowData",(i+1),
                                    				{ "SID":"", "CITYCODE":"", "AGTNAME":"", "COMMUNITYNAME":"", "INSTALLLOCKNAME":"","INSTALLTIME":"","OTIME":"no","STATUS":"no","PID":"","CHECKSTATUS":"no"}
                            				,"");
                            			}
                            			$(".ui-paging-info").html(re_records-num+1 +" - "+re_records+"　共 "+re_records+" 条");
                            		}
                            	}
                            });
                }else{

                	$("#jqGrid").jqGrid(
                            {
                                url: "${pageContext.request.contextPath}/admin/tlocks_page.action?type=1",
                                mtype: 'POST',
                                datatype: "json",
                                height: 350,
                                autowidth: true,
                                shrinkToFit: true,
                                rowNum: 10,
                                rowList: [10, 20, 30],
                                colNames: ['序号', '所属区域', '公司', '主用户',
                                     '安装状态', '检测状态','PID ','激活码','安装时间','操作'],
                                colModel: [{
                                    name: "SID",
                                    index: "SID",
                                    key: true,
                                    hidden: true
                                }, {
                                    name: 'CITYCODE',
                                    index: 'CITYCODE',
                                    width: 90
                                }, {
                                    name: 'AGTNAME',
                                    index: 'AGTNAME',
                                    width: 80
                                },{
                                    name: 'COMMUNITYNAME',
                                    index: 'COMMUNITYNAME',
                                    width: 80
                                }, {
                                    name: "STATUS",
                                    index: "STATUS",
                                    width: 80,
                                    formatter: function (cellvalue, options, rawObject) {

                                    	if(cellvalue=='no'){
                                        	return '';
                                        }else if(cellvalue=='0'){
                                        	return '未安装';
                                        }else if(cellvalue == '1'){
                                         	return '已安装';
                                        }else if(cellvalue == '2'){
                                         	return '锁厂测试安装';
                                        }else if(cellvalue == '-1' && rawObject.AGTCODE ==''){
                                        	return '未发货';
                                        }else if(cellvalue == '-1' && rawObject.AGTCODE !=''){
                                        	return '已分配到公司';
                                        }else{
                                        	return '';
                                        }
                                    }
                                }, {
                                    name: "CHECKSTATUS",
                                    index: "CHECKSTATUS",
                                    width: 40,
                                    align:'center',
                                    formatter: function (cellvalue, options, rawObject) {
                                    	if(cellvalue=='no'){
                                        	return '';
                                        }else if(cellvalue=='0'){
                                        	return '未检测';
                                        }else if(cellvalue=='1'){
                                        	return '合格';
                                        }else if(cellvalue == '2'){
                                         	return '不合格';
                                        }else{
                                        	return '';
                                        }
                                    }
                                },{
                                    name: 'PID',
                                    index: 'PID',
                                    width: 80
                                },{
                                    name: 'CHECKCODE',
                                    index: 'CHECKCODE',
                                    width: 100
                                },{
                                    name: "INSTALLTIME",
                                    index: "INSTALLTIME",
                                    width: 80
                                },{
                                    name: 'CHECKSTATUS',
                                    index: 'CHECKSTATUS',
                                    width: 140,
                                    formatter: function (cellvalue, options, rawObject) {
                                    	if(cellvalue=='no'){
                                        	return '';
                                        }else if('${userRole}'==1 || '${userRole}'== 2){
                                        	if(cellvalue=='0' && rawObject.STATUS=='2'){
                                        		return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                       + rawObject.SID + ',1)"/><input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                       + rawObject.SID + ',2)"/>';
                                            }else if(cellvalue=='1' && rawObject.STATUS=='2'){
                                                return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                            }else if(cellvalue=='2' && rawObject.STATUS=='2'){
                                                return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                        + rawObject.SID + ',1)"/>';
                                            }else{
                                            	return '';
                                            }
                                        }else if('${userRole}'==3 || '${userRole}'== 4){
                                        	if(cellvalue=='0' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifUp){
                                                   value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                       + rawObject.SID + ',1)"/>';
                                                }
                                                if(ifDown){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                                }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else if(cellvalue=='1' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifDown){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="不合格" onclick="check('
                                                        + rawObject.SID + ',2)"/>';
                                                }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else if(cellvalue=='2' && rawObject.STATUS=='2'){
                                                var value='';
                                                if(ifUp){
                                                    value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="合格 " onclick="check('
                                                        + rawObject.SID + ',1)"/>';
                                                 }
                                                if(ifTestKey){
                                                	value +='<input type="button" style="padding: 2px 10px" class="btn btn-success" value="测试钥匙" onclick="testKey('
                                                        + rawObject.SID + ')"/>';
                                                }
                                            	return value;
                                            }else{
                                            	return '';
                                            }
                                        }else if(('${userRole}'==5 || '${userRole}'== 6) && ifTestKey){
                                        	return '<input type="button" style="padding: 2px 10px" class="btn btn-success" value="发放钥匙" onclick="testKey2('
                                                    + rawObject.SID + ')"/>';
                                        }else{
                                        	return '';
                                        }
                                 	}
                                }],
                                sortname: 'status desc,sid',
                                sortorder:'desc',
                                rownumbers: true,
                                jsonReader: {
                                    root: 'rows',
                                    page: "page",
                                    total: 'total',
                                    records: "records",
                                    repeatitems: false
                                },
                                pager: "#jqGrid_paper",
                                viewrecords: true,
                                multiselect: true,
                                hidegrid: false,
                                loadComplete: function(){
                            		var re_records = Number($("#jqGrid").getGridParam('records'));
                            		var page = Number($("#jqGrid").getGridParam('page'));
                            		var rows=Number($("#jqGrid").getGridParam('rowNum'));
                            		var num = re_records-(page-1)*rows;
                            		if(num < rows)
                            		{
                            			for(var i=num;i<rows;i++){
                            				$("#jqGrid").jqGrid("addRowData",(i+1),
                                    				{ "SID":"", "CITYCODE":"", "AGTNAME":"", "COMMUNITYNAME":"", "INSTALLLOCKNAME":"","INSTALLTIME":"","OTIME":"no","STATUS":"no","PID":"","CHECKSTATUS":"no"}
                            				,"");
                            			}
                            			$(".ui-paging-info").html(re_records-num+1 +" - "+re_records+"　共 "+re_records+" 条");
                            		}
                            	}
                            });


                }





                if('${userRole}' == '1' || '${userRole}' == '2' || '${userRole}' == '21'){
	                $.post("${pageContext.request.contextPath}/admin/tagent_list.action", function (data) {
			            $("#agtCode").empty();
			            $("#agtCode").append("<option value=''>请选择公司</option>");
			            for (var i = 0; i < data.length; i++) {
			                $("#agtCode").append("<option value='" + data[i].agtCode + "'>" + data[i].agtName + "</option>");
			            }
			            $("#agtCode").select2({
			                "placeholder": "请选择公司",
			                "width": "100%"
			            })
			            $("#agtCode").change(function () {
			            	var data = $(this).val();
			            	$.post("${pageContext.request.contextPath}/admin/tcommunity_list.action", {"agtCode": data},function (data) {
					            $("#communityId").empty();
					            $("#communityId").append("<option value=''>请选择</option>");
					            for (var i = 0; i < data.length; i++) {
					                $("#communityId").append("<option value='" + data[i].sid + "'>" + data[i].communityName + "</option>");
					            }
					            $("#communityId").select2({
					                "placeholder": "请选择",
					                "width": "100%"
					            })
					        }, "json");
			            })
			        }, "json");
                }else if('${userRole}' == '3' || '${userRole}' == '4' ){
                	$.post("${pageContext.request.contextPath}/admin/tcommunity_list.action",
                        function (data) {
			            $("#communityId").empty();
			            $("#communityId").append("<option value=''>请选择</option>");
			            for (var i = 0; i < data.length; i++) {
			                $("#communityId").append("<option value='" + data[i].sid + "'>" + data[i].communityName + "</option>");
			            }
			            $("#communityId").select2({
			                "placeholder": "请选择",
			                "width": "100%"
			            })
			        }, "json");
                }
                <!--查询-->
                jQuery("#query").click(query);
                jQuery("#resetForm").click(resetForm);
            });
    $("#status").select2({
    	 "placeholder": "请选择",
         "minimumResultsForSearch":-1
    })
     $("#checkstatus").select2({
    	 "placeholder": "请选择",
         "minimumResultsForSearch":-1
    })

    <!--刷新jqGrid表格数据-->
    function refreshGrid() {
        $("#jqGrid").trigger("reloadGrid");
    }
    function resetForm() {
        location.reload();
        return false;
    }
    function query() {
        var $agtCode = $("#agtCode").val();
        var $communityId = $("#communityId").val();
        var $areaCode;
		if('${userRole}' != 5 && '${userRole}' != 6){
	        var selects=document.getElementsByName('location');
	        $areaCode = selects[0].value;
		}
        var $pid = $("#pid").val();
        var $status = $("#status").val();
        var $pid1 = $("#pid1").val();
        var $pid2 = $("#pid2").val();
        var $checkstatus = $("#checkstatus").val();
        $("#jqGrid").jqGrid('setGridParam', {
            url: "${pageContext.request.contextPath}/admin/tlocks_page.action",
            postData: {
            	'type':1,
                'agtCode': $agtCode,
                'communityId': $communityId,
                'areaCode': $areaCode,
                'pid': $pid,
                'status': $status,
                'checkstatus': $checkstatus,
                'pid1': $pid1,
                'pid2': $pid2
            }, //发送数据
            page: 1
        }).trigger("reloadGrid"); //重新载入*/
        return false;
    }
    function check(sid,type){
        if(type == 1){
        	layer.confirm('确认所选择的记录检验为合格', function (index) {
                $.post("${pageContext.request.contextPath}/admin/tlocks_check.action", {
					"ids":sid,
                    "type": 1
                }, function (result) {
                    result = eval("(" + result + ")");
                    layer.alert(result.msg);
                    refreshGrid();
                });
                layer.close(index);
            });
        }else{
        	layer.confirm('确认所选择的记录检验为不合格', function (index) {
                $.post("${pageContext.request.contextPath}/admin/tlocks_check.action", {
                	  "ids":sid,
                	  "type": 2
                }, function (result) {
                    result = eval("(" + result + ")");
                    layer.alert(result.msg);
                    refreshGrid();
                });
                layer.close(index);
            });
        }

    }
    function testKey(sid){
    	layer.open({
            type: 2,
            title: ['发放测试钥匙', 'font-weight:bold'],
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '430px'],
            scrollbar: false,
            content: '${pageContext.request.contextPath}/admin/tlocks_testKey.action?sid='+sid,
            btn: ['提交', '取消'],
            yes: function (index, layero) {
                var options = {
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data.code == 1) {
                            layer.alert(data.msg, function () {
                                layer.closeAll();
                                refreshGrid();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    }
                };
                var tel = $("#layui-layer-iframe" + index).contents().find("#tel").val();
                var userName = $("#layui-layer-iframe" + index).contents().find("#userName").val();
               	if(!(/^1[3|4|5|7|8]\d{9}$/.test(tel))){
               		alert("手机号格式输入错误");
               	}else if(!userName){
               		alert("请输入姓名");
                }else{
               	 	$("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
              		$("#layui-layer-iframe" + index).contents().find("#form").submit();
               	}
            },
            cancel: function (index, layero) {
                layer.close(index);
            }
        });
    }
    $("#communityId").select2({
        "placeholder": "请选择社区",
        "minimumResultsForSearch":-1
    });
    function testKey2(sid){
    	layer.open({
            type: 2,
            title: ['发放住户钥匙', 'font-weight:bold'],
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '430px'],
            scrollbar: false,
            content: '${pageContext.request.contextPath}/admin/tlocks_testKey.action?sid='+sid,
            btn: ['提交', '取消'],
            yes: function (index, layero) {
                var options = {
                    success: function (data) {
                        data = JSON.parse(data);
                        if (data.code == 1) {
                            layer.alert(data.msg, function () {
                                layer.closeAll();
                                refreshGrid();
                            });
                        } else {
                            layer.alert(data.msg);
                        }
                    }
                };
                var tel = $("#layui-layer-iframe" + index).contents().find("#tel").val();
                var userName = $("#layui-layer-iframe" + index).contents().find("#userName").val();
                var validity = $("#layui-layer-iframe" + index).contents().find("#validity").val();

               	if(!(/^1[3|4|5|7|8]\d{9}$/.test(tel))){
               		alert("手机号格式输入错误");
               	}else if(!userName){
               		alert("请输入姓名");
                }else if(!validity){
               		alert("请选择钥匙到期时间");
                }else{
               	 	$("#layui-layer-iframe" + index).contents().find("#form").ajaxForm(options);
              		$("#layui-layer-iframe" + index).contents().find("#form").submit();
               	}
            },
            cancel: function (index, layero) {
                layer.close(index);
            }
        });
    }

</script>
</body>
</html>
